﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace automobileinventory1
{
    public partial class Form1 : Form
    {
        SqlConnection sqlconProductDetails;
        SqlCommand sqlcomProductDetails;
        SqlDataReader sqldreaderProductDetails;
        string connectionString;
        int rownumber= 0;

        public Form1()
        {
            InitializeComponent();
            connectionString = "Data Source= localhost; Database=AutomobileInventory; Integrated Security=SSPI";
            sqlconProductDetails = new SqlConnection(connectionString);
            sqlcomProductDetails = new SqlCommand();
        }
            
        private void Form1_Load(object sender, EventArgs e)
        {
            if (sqlconProductDetails.State == ConnectionState.Open)
                sqlconProductDetails.Close();
            sqlconProductDetails.Open();
            sqlcomProductDetails.CommandText = "SELECT * FROM ProductDetails";
            sqlcomProductDetails.Connection = sqlconProductDetails;
            sqldreaderProductDetails = sqlcomProductDetails.ExecuteReader();
            if (sqldreaderProductDetails.HasRows)
            {
                sqldreaderProductDetails.Read();
                rownumber = rownumber + 1;
                DisplayResults();
                if (sqldreaderProductDetails.Read())
                {
                    btn_next.Enabled = true;
                    btn_first.Enabled = false;
                    btn_previous.Enabled = false;
                    btn_last.Enabled = true;
                }
                else
                {
                    btn_first.Enabled = false;
                    btn_previous.Enabled = false;
                    btn_next.Enabled = false;
                    btn_last.Enabled = false;
                }
            }
                              
        }
        void DisplayResults()
        {
            txt_model.Text = sqldreaderProductDetails.GetString(0);
            txt_manufacturer.Text = sqldreaderProductDetails.GetString(1);
            txt_builtyear.Text = sqldreaderProductDetails.GetInt32(2).ToString();
            txt_VIN.Text = sqldreaderProductDetails.GetString(3);
            txt_cylinders.Text = sqldreaderProductDetails.GetInt32(4).ToString();
            txt_color.Text = sqldreaderProductDetails.GetString(5);
            txt_enginenumber.Text = sqldreaderProductDetails.GetInt32(6).ToString();
            txt_price.Text = sqldreaderProductDetails.GetInt32(7).ToString();
        }

       
        void clearcontrol()
        {
            txt_builtyear.Text="";
            txt_color.Text="";
            txt_cylinders.Text="";
            txt_enginenumber.Text="";
            txt_manufacturer.Text="";
            txt_model.Text="";
            txt_price.Text="";
            txt_VIN.Text="";
        }
        
        private void btn_next_Click(object sender, EventArgs e)
        {
            if (sqldreaderProductDetails.Read())
            {
                DisplayResults();
                rownumber = rownumber + 1;
            }
            else
            {
                MessageBox.Show("This is the last record !", "Product Details", MessageBoxButtons.OK);
                btn_next.Enabled = false;
                btn_last.Enabled = false;
            }
            btn_first.Enabled = true;
            btn_last.Enabled = true;
        }

        private void btn_last_Click(object sender, EventArgs e)
        {
            while (sqldreaderProductDetails.Read())
            {
                DisplayResults();
                rownumber = rownumber + 1;
            }
            btn_first.Enabled = true;
            btn_previous.Enabled = true;
        }

        private void btn_first_Click(object sender, EventArgs e)
        {
            if (!sqldreaderProductDetails.IsClosed)
                sqldreaderProductDetails.Close();
            sqldreaderProductDetails = sqlcomProductDetails.ExecuteReader();
            if (rownumber != 0)
            {
                sqldreaderProductDetails.Read();
                DisplayResults();
            }
            else
            {
                MessageBox.Show("This is the first record", "Product Details", MessageBoxButtons.OK);
            }
        }

        private void btn_previous_Click(object sender, EventArgs e)
        {

        }

        
        
    }
}
